Skip to content

Fix colormap handling for Matplotlib 3.11 - #4

Open
HugoFara wants to merge 3 commits into
QuentinRodier:mainfrom
HugoFara:compat/matplotlib-colormaps
Open

Fix colormap handling for Matplotlib 3.11#4
HugoFara wants to merge 3 commits into
QuentinRodier:mainfrom
HugoFara:compat/matplotlib-colormaps

Conversation

@HugoFara

Copy link
Copy Markdown

matplotlib.cm.get_cmap was removed in Matplotlib 3.11, and setup.py allows matplotlib>=3.10.9 uncapped — so a fresh install breaks on every filled plot:

AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

Replaced with matplotlib.colormaps[name].resampled(lut) behind a small helper that keeps the original's handling of None and of Colormap instances (psectionV/psectionH feed one back in through Lcolormap[i]). Also swaps set_under/set_over, pending deprecation in 3.11, for Colormap.with_extremes.

Output is bit-identical, checked on six colormaps of both classes. Verified on 3.11.1 and 3.10.9 with warnings as errors.

Meso-NH vendors a byte-identical copy of this file at src/LIB/Python/, broken the same way.

addWhitecm and addColorcm called cm.get_cmap(name, 256), deprecated in
Matplotlib 3.7 and removed in 3.11:

    AttributeError: module 'matplotlib.cm' has no attribute 'get_cmap'

setup.py requires matplotlib>=3.10.9 with no upper bound, so a fresh
install resolves to 3.11 and every colour-filled plot raises.

The documented replacement for get_cmap(name, lut) is
matplotlib.colormaps[name].resampled(lut). Two details of the old
behaviour are worth keeping, so this goes through a small helper:

- get_cmap also accepted a Colormap instance and returned it unchanged.
  psectionV and friends assign the result back into Lcolormap[i], so a
  reused list feeds a Colormap in on the next call.
- resampled() returns a new object, so the set_under/set_over calls in
  addColorcm no longer risk mutating the registered colormap.

Verified against matplotlib 3.11.1: colormap by name, Colormap
passthrough, addColorcm, addWhitecm, and a Colormap fed back in.

The same two calls exist in Meso-NH's copies of this module
(src/LIB/Python and examples/integration_cases/hpc/OCEAN_LES/005_python)
and are broken there too.
Matplotlib 3.11 marks both as pending deprecation:

    PendingDeprecationWarning: The set_under function will be deprecated
    in a future version. Use cmap.with_extremes(under=...) or
    Colormap(under=...) instead.

with_extremes returns a new colormap rather than mutating in place,
which also removes the last way addColorcm could modify a Colormap
handed to it by the caller. Available since Matplotlib 3.4, so it is
safe against the declared floor of 3.10.9.

Verified on 3.11.1 and 3.10.9 with warnings raised as errors.
The removed matplotlib.cm.get_cmap mapped name=None to
rcParams['image.cmap']. The helper did not, so an explicit None in
Lcolormap would raise KeyError where it previously plotted with the
default colormap.

Lcolormap is seeded with 'gist_rainbow_r' when empty, so None only
arrives if a caller passes it, but this keeps the replacement a strict
drop-in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant